include_once dirname(__FILE__) . '/main.class.php';
 
class Thread extends Main
{
	public $Next = "";
	public $Previous = "";
	public $First = "";
	public $Last = "";
	public $A_index;
 
	public $Pimage;
	public $Pusername;
	public $Pcontent;
	public $Puser_registration;
	public $Puser_messages;
	public $Pmessage_data;
	public $PreplyLink;
 
	public $Tid;
	public $Ttitle;
	public $Tusername;
	public $Tusername2;
	public $Tdate;
	public $Tdate2;
	public $Tpost;
 
		public function SaveNewThread()
		{
			$title = mysql_real_escape_string($_POST['title']);
			$content = mysql_real_escape_string($_POST['content']);
			$time = time();
			$sql = "INSERT INTO thread (title,start_user,start_date,last_date) VALUES ('$title',$_SESSION[user_id],$time,$time)";
 
			mysql_query($sql, $this->conn);
			$id = mysql_insert_id($this->conn);
 
			$sql2 = "INSERT INTO posts (id_th,id_user,p_date,content) VALUES ($id,$_SESSION[user_id],$time,'$content')";
			mysql_query($sql2,$this->conn);
 
			$sql3 = "UPDATE users SET score=score+1 WHERE id=$_SESSION[user_id]";
			mysql_query($sql3, $this->conn);
 
			header("Location: showthread.php?id=$id");
 
			return;
		}
 
		public function ShowThread()
		{
			$sql = $this->Pagination("SELECT * FROM thread,users WHERE thread.start_user=users.id ORDER BY thread.last_date DESC", "thread", 10);
			$res = mysql_query($sql, $this->conn);
 
			while($row = mysql_fetch_array($res))
			{
				$query = "SELECT * FROM posts,users WHERE posts.id_th=$row[id_th] AND posts.id_user=users.id ORDER BY p_date DESC";
				$res2 = mysql_query($query, $this->conn);
				$row2 = mysql_fetch_array($res2);
				$this->Tid = $row['id_th'];
				$this->Ttitle = stripslashes($row['title']);
				$this->Tusername = stripslashes($row['username']);
				$this->Tdate = $this->CreateDateByTimestamp($row['start_date']);
				$this->Tpost = $row['n_post'];
				$this->Tusername2 = stripslashes($row2['username']);
				$this->Tdate2 = $this->CreateDateByTimestamp($row2['p_date']);
 
				include 'template/' . TEMPLATE_NAME . '/thread.php';
			}
 
 
			echo file_get_contents('template/' . TEMPLATE_NAME .'/thread_footer.php');
			echo $this->ShowPaginationLink();
 
		}
 
		public function ShowSingleThread()
		{
			$sql = $this->Pagination("SELECT * FROM users,posts WHERE posts.id_th=$_GET[id] AND posts.id_user=users.id ORDER BY posts.p_date ASC", "posts", 10);
			$res = mysql_query($sql, $this->conn);
 
 
			while($row = mysql_fetch_array($res))
			{
				$this->Pusername = stripslashes($row['username']);
				$this->Pimage = $row['image'];
				$this->Puser_registration = $this->CreateDateByTimestamp($row['s_date']);
				$this->Pmessage_data = $this->CreateDateByTimestamp($row['p_date']);
				$this->Puser_messages = $row['score'];
				$this->Pcontent = stripslashes($row['content']);
				$this->PreplyLink = '<a href="newpost.php?id=' . $row['id_th'] . '" id="link">[rispondi]</a>';
 
 
 
				include('template/' . TEMPLATE_NAME . '/single_post.php');
			}
 
			echo $this->ShowPaginationLink();
		}
 
		public function ShowSinglePost()
		{
			$sql = "SELECT * FROM users,posts WHERE posts.id=$_GET[idpost] AND posts.id_user=users.id";
			$res = mysql_query($sql, $this->conn);
 
			$row = mysql_fetch_array($res);
 
			$this->Pusername = stripslashes($row['username']);
			$this->Pimage = $row['image'];
			$this->Puser_registration = $this->CreateDateByTimestamp($row['s_date']);
			$this->Pmessage_data = $this->CreateDateByTimestamp($row['p_date']);
			$this->Puser_messages = $row['score'];
			$this->Pcontent = stripslashes($row['content']);
			$this->PreplyLink = '<a href="newpost.php?id=' . $row['id_th'] . '" id="link">[rispondi]</a>';
 
			include('template/' . TEMPLATE_NAME . '/single_post.php');
 
		}
 
		public function SaveNewPost()
		{
			if($_POST['content'] == "")
			{
				header("Location: newpost.php?id=$_POST[id]&alert=1");
				die;
			}
			$content = mysql_real_escape_string($_POST['content']);
			$time = time();
 
			$sql = "INSERT INTO posts (id_th,id_user,p_date,content) VALUES ($_POST[id],$_SESSION[user_id],$time,'$content')";
			$sql2 = "UPDATE users SET score=score+1 WHERE id=$_SESSION[user_id]";
			$sql3 = "UPDATE thread SET last_date=$time,n_post=n_post+1 WHERE id_th=$_POST[id]";
			mysql_query($sql, $this->conn);
			mysql_query($sql2, $this->conn);
			mysql_query($sql3, $this->conn);
 
			$sql4 = "SELECT id,email,username FROM users WHERE mailer=1 AND id!=$_SESSION[user_id] AND id IN (SELECT id_user FROM posts WHERE id_th=$_POST[id])";
			$res = mysql_query($sql4, $this->conn);
 
			include_once 'lib/messages.class.php';
			$msg = new ForumMailer();
 
			$body = "Buongiorno,<br>";
			$body .= $this->TheUser() . ' ';
			$body .= "ha appena replicato alla discussione ";
			$body .= '<i><b>' . $this->ThreadTitle($_POST['id']) . '</i></b> alla quale sei iscritto.<br>';
			$body .= "Per visualizzare questa discussione clicca sul seguente link.<br><br>";
			$body .= '<a href="' . SYSTEM_URL . 'showthread.php?id=' . $_POST['id'] . '">[visualizza thread]</a>'; 
 
			while ($row = mysql_fetch_array($res))
			{
				$msg->MailImplementsAndSend($row['email'], 'Nuova risposta sul forum', 'Nuova risposta', $body);
			}
 
 
 
			header("Location: showthread.php?id=$_POST[id]");
			return;
		}
 
		protected function Pagination($query, $table, $step)
		{
			if(!isset($_GET['start']) OR $_GET['start'] < 0)
			{
				$start = 0;
			}
			else
			{
				$start = $_GET['start'];
			}
 
			if(isset($_GET['id']))
			{
				$id = $_GET['id'];
			}
			else
			{
				$id = "";
			}
 
			if($id)
			{
				$cond = " WHERE id_th=$_GET[id]";
			}
			else
			{
				$cond = "";
			}
 
			$page = basename($_SERVER['PHP_SELF']);
 
			$sql = "SELECT count(*) AS tot FROM $table" . $cond;
			$res = mysql_query($sql, $this->conn);
			$row = mysql_fetch_array($res);
			$max = $row['tot'];
 
			if(($start + $step) < $max)
			{
				$next = $start + $step;
				$this->Next = "<a href=\"$page?start=$next&id=$id\"  id=\"link\">&gt;</a>";
				$last = intval($max / $step) * $step;
				$this->Last = "<a href=\"$page?start=$last&id=$id\" id=\"link\">&gt;&gt;</a>";
 
			}
			if($start > 0)
			{
				$back = $start - $step;
				$this->Previous = "<a href=\"$page?start=$back&id=$id\" id=\"link\">&lt;</a>";
				$this->First = "<a href=\"$page?start=0&id=$id\" id=\"link\">&lt;&lt;</a>";
			}
 
			$s1 = $start + 1;
			$s2 = $start + $step;
 
			if($max - $start < $step)
			{
				$s2 = $max;
			}
 
			$this->A_index = "da $s1 a $s2 di $max";
 
			$sql2 = $query . " LIMIT $start,$step";
 
			return $sql2;
 
		}
 
		protected function ShowPaginationLink()
		{
			include 'template/' . TEMPLATE_NAME . '/pagination.php';
			return;
		}
 
		public function ThreadTitle($id)
		{
			$sql = "SELECT title FROM thread WHERE id_th=$id";
			$res = mysql_query($sql, $this->conn);
			$row = mysql_fetch_array($res);
			return stripslashes($row['title']);
		}
 
		public function CompositeContent()
		{
			$page = basename($_SERVER['PHP_SELF']);
 
			switch($page)
			{
				case "index.php":
				$this->ShowThread();
				break;
 
				case "showthread.php":
				$this->ShowSingleThread();
				break;
 
				case "newpost.php":
				include 'template/' . TEMPLATE_NAME . '/new_post_form.php';
				break;
 
				case "show_post.php":
				$this->ShowSinglePost();
				break;
			}
		}
 
}